package com.example.secondhandmall.activity;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.secondhandmall.R;
import com.example.secondhandmall.api.Api;
import com.example.secondhandmall.api.ApiConfig;
import com.example.secondhandmall.api.MyCallback;
import com.example.secondhandmall.entity.LoginResponse;
import com.example.secondhandmall.entity.PwdSwitch;
import com.example.secondhandmall.util.StringUtils;
import com.google.gson.Gson;

import java.util.HashMap;

public class RegisterActivity extends BaseActivity {

    private EditText etAccount;
//    private EditText etName;
    private Button btnRegister;

    private PwdSwitch pwd1;
    private PwdSwitch pwd2;

    @Override
    protected int initLayout() {
        return R.layout.activity_register;
    }

    @Override
    protected void initView() {
        etAccount = findViewById(R.id.et_account);
//        etName = findViewById(R.id.et_name);
        pwd1 = new PwdSwitch(findViewById(R.id.et_pwd), findViewById(R.id.iv_pwd_switch1));
        pwd2 = new PwdSwitch(findViewById(R.id.et_confirm_pwd), findViewById(R.id.iv_pwd_switch2));
        btnRegister = findViewById(R.id.btn_register);
    }

    @Override
    protected void initData() {
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account = etAccount.getText().toString().trim();
//                String name = etName.getText().toString().trim();
                String pwd = pwd1.etPwd.getText().toString().trim();
                String confirmPwd = pwd2.etPwd.getText().toString().trim();
                register(account, pwd, confirmPwd);
            }
        });
    }

    //注册
    private void register(String account, String pwd, String confirmPwd) {
        if (StringUtils.isEmpty(account)) {
            showToast("请输入账号!");
            return;
        }
        if (StringUtils.isEmpty(pwd)) {
            showToast("请输入密码!");
            return;
        }
        if (!pwd.equals(confirmPwd)) {
            showToast("两次输入密码不一致!");
            return;
        }

        //网络请求
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("userId", account);
//        params.put("name", name);
        params.put("passWord", pwd);
        Api.config(ApiConfig.REGISTER, params).postRequest(new MyCallback() {
            @Override
            public void onSuccess(String res) {
                Log.e("onSuccess", res);

                //把json字符串转转换为实体对象
                Gson gson = new Gson();
                LoginResponse registerResponse = gson.fromJson(res, LoginResponse.class);
                if (registerResponse.getCode() == 200) {
                    if(registerResponse.isData()){
                    navigateTo(LoginActivity.class);
                    showToastSync("注册成功！");}else {
                        showToastSync("该账号已被注册!");
                    }
                } else {
                    showToastSync("注册失败！");
                }
            }

            @Override
            public void onFailure(Exception e) {
                Log.e("onFailure", e.toString());
                showToastSync("请求失败！");
            }
        });
    }
}